package com.link.play.fit.ai.ui.vip

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.addCallback
import androidx.lifecycle.lifecycleScope
import com.android.billingclient.api.ProductDetails
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.databinding.AcVipBinding
import com.link.play.fit.ai.ext.d
import com.link.play.fit.ai.utils.GooglePayUtil.extractAndCalculateDailyPriceToMonth
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class VipAc : BaseActivity<AcVipBinding>(AcVipBinding::inflate) {
    private var yearSubscriptionOfferDetails: List<ProductDetails.SubscriptionOfferDetails>? = null
    private var yearProductDetails: ProductDetails? = null
    private var yearFormattedPrice = ""

    private var monthSubscriptionOfferDetails: List<ProductDetails.SubscriptionOfferDetails>? = null
    private var monthProductDetails: ProductDetails? = null
    private var monthFormattedPrice = ""
    private var isChoseYear = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)

        initListener()
        binding.llVip.visibility = View.VISIBLE
        onBackPressedDispatcher.addCallback {
           "finish".d()
            goToAc(VipOffAc::class.java){
                putExtra("originalPrice",yearFormattedPrice)
            }
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.llYear.setOnClickListener {
            yearUI()
        }
        binding.llMonth.setOnClickListener {
            monthUI()
        }

        binding.llYear.callOnClick()
        binding.tvBtn.setOnClickListener {
//            goToAc(VipOffAc::class.java)
            if(isChoseYear){
//                yearSubscriptionOfferDetails?.forEach {
//
//                    if (it.basePlanId == SUB_YEAR_PLAN_ID) {
//                        if(it.pricingPhases.pricingPhaseList[0].formattedPrice=="免费"){
//                            "offerToken  ${it.offerToken}".d()
//                            GooglePayUtil.pay(this, it.offerToken, yearProductDetails!!)
//                        }
//                    }
//                }
            }else{
//                monthSubscriptionOfferDetails?.forEach {
//                    if (it.basePlanId == SUB_MONTH_PLAN_ID) {
//                        GooglePayUtil.pay(this, it.offerToken, monthProductDetails!!)
//                    }
//                }
            }
        }
    }

    @SuppressLint("DefaultLocale", "StringFormatMatches")
    private fun initListener(){

        lifecycleScope.launch {
//            yearProductDetails = GooglePayUtil.queryProduct(SUB_YEAR_product_ID)
            yearSubscriptionOfferDetails = yearProductDetails?.subscriptionOfferDetails
            yearProductDetails?.subscriptionOfferDetails?.forEach {
                val pricingPhase = it.pricingPhases.pricingPhaseList[0]
                "it.basePlanId  ${it.basePlanId}".d()

                when (it.basePlanId) {

//                    SUB_YEAR_PLAN_ID -> {
//                        yearFormattedPrice = pricingPhase.formattedPrice
//                        binding.tvPerYear.text = getString(
//                            R.string.yearly_mo,
//                            extractAndCalculateDailyPriceToMonth(yearFormattedPrice)
//                        )
//                        binding.tvPer.text = getString(R.string._3_days_free_then_per_year_mo, yearFormattedPrice,  extractAndCalculateDailyPriceToMonth(yearFormattedPrice))
//                    }
                }
            }
        }

        lifecycleScope.launch {
//            monthProductDetails = GooglePayUtil.queryProduct(SUB_MONTH_PRODUCT_ID)
//            monthSubscriptionOfferDetails = monthProductDetails?.subscriptionOfferDetails
//            monthProductDetails?.subscriptionOfferDetails?.forEach {
//                val pricingPhase = it.pricingPhases.pricingPhaseList[0]
//
//                "monthProductDetails.formattedPrice ${ pricingPhase.formattedPrice} priceAmountMicros ${ pricingPhase.priceAmountMicros}".d()
//                when (it.basePlanId) {
//                    SUB_MONTH_PLAN_ID -> {
//                        monthFormattedPrice = pricingPhase.formattedPrice
//                        binding.tvPerMonth.text = getString(
//                            R.string.monthly_mo,
//                            monthFormattedPrice
//                        )
//                    }
//                }
//            }
        }
    }

    @SuppressLint("StringFormatMatches")
    private fun monthUI() {
        binding.clYear.visibility = View.GONE
        binding.clMonth.visibility = View.VISIBLE
        binding.tvTitle.text = getString(R.string.to_nreach_faster)
        binding.tvBtn.text = getString(R.string.start_my_journey)
        isChoseYear = false
        "monthFormattedPrice $monthFormattedPrice".d()
        if(monthFormattedPrice.isNotEmpty()){
            binding.tvPer.text = getString(R.string.just_per_month, monthFormattedPrice)

        }
        binding.tvPayment.text = getString(R.string.no_commitment_cancel_anytime)

        binding.llYear.isSelected = false
        binding.llMonth.isSelected = true
    }

    @SuppressLint("StringFormatMatches")
    private fun yearUI() {
        isChoseYear = true

        binding.clYear.visibility = View.VISIBLE
        binding.clMonth.visibility = View.GONE
        binding.tvTitle.text = getString(R.string._3_day_nfree)
        binding.tvBtn.text = getString(R.string.start_my_3_day_free_trial)
        "yearFormattedPrice $yearFormattedPrice".d()

        if(yearFormattedPrice.isNotEmpty()){
            binding.tvPer.text = getString(R.string._3_days_free_then_per_year_mo, yearFormattedPrice,  extractAndCalculateDailyPriceToMonth(yearFormattedPrice))

        }
        binding.tvPayment.text = getString(R.string.no_payment_due_now)

        binding.llYear.isSelected = true
        binding.llMonth.isSelected = false
    }

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: String) {
        if(event == "isVip"){
            finish()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}